﻿using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Library;
using System.Windows.Forms.Design;

namespace ProjectBase.Guidance.Helpers
{
    public static class VisualStudioHelper
    {
        public static string CurrentTargetPath(DTE dte)
        {
            string ctp = String.Empty;
            try
            {
                if(dte.SelectedItems.Count == 0)
                {
                    return "nothing.xml";
                }
                object target = DteHelper.GetTarget(dte);

                ProjectItem projectItem = target as ProjectItem;

                if (projectItem != null)
                {
                    ctp = SolutionHelper.GetTargetPathOfProjectItem(projectItem).Replace("\\", "/");
                }
                else if (target is Project)
                {
                    ctp = String.Empty;
                }
            }
            catch
            {
                ctp = "error.xml";
            }
            return ctp;
        }

        public static bool IsSolutionExplorerActive(DTE dte)
        {
            return (dte.ActiveWindow == dte.Windows.Item(Constants.vsWindowKindSolutionExplorer));
        }

        public static string CurrentTargetName(DTE dte)
        {
            string csn = String.Empty;
            object target = DteHelper.GetTarget(dte);

            ProjectItem projectItem = target as ProjectItem;

            if (projectItem != null)
            {
                csn = projectItem.Name;
            }
            else
            {
                Project project = target as Project;
                if (project != null)
                {
                    csn = project.Name;
                }
            }

            return csn;
        }

        /// <summary>
        /// Creates string from folder names (firstfolder.secondfolder.thirdfolder ...)
        /// depending on current item
        /// </summary>
        /// <param name="dte">current project</param>
        /// <returns></returns>
        public static string CurrentTargetRecursiveName(DTE dte)
        {
            return CurrentTargetRecursiveName(dte, string.Empty);
        }
        /// <summary>
        /// Creates string from folder names (firstfolder.secondfolder.thirdfolder ...)
        /// depending on current item. If clicked on project (root without folders)
        /// the default string is returned
        /// </summary>
        /// <param name="dte">current project</param>
        /// <param name="defaultNamespace"></param>
        /// <returns></returns>
        public static string CurrentTargetRecursiveName(DTE dte, string defaultNamespace)
        {
            string csn = defaultNamespace;
            object target = DteHelper.GetTarget(dte);

            ProjectItem projectItem = target as ProjectItem;

            if (projectItem != null)
            {
                csn = ParentName(projectItem);
            }
            return csn;
        }

        /// <summary>
        /// Recursively tries to find all parentfolder of current object
        /// and create the namespace form their names
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private static string ParentName(ProjectItem item)
        {
            string parentName = item.Name;
            ProjectItem parent = item.Collection.Parent as ProjectItem;
            if (parent != null)
            {
                parentName = ParentName(parent) + "." + item.Name;
            }
            return parentName;
        }

        public static string CurrentTargetParentName(DTE dte)
        {
            string csn = String.Empty;
            object target = DteHelper.GetTarget(dte);

            ProjectItem projectItem = target as ProjectItem;

            if (projectItem != null)
            {
                ProjectItem parentItem = projectItem.Collection.Parent as ProjectItem;
                csn = parentItem.Name;
            }
            else
            {
                csn = "";
            }

            return csn;
        }
        public static ProjectItem CurrentlySelectedProjectItem(DTE dte)
        {
            return DteHelper.GetTarget(dte) as ProjectItem;
        }

        public static string CurrentTargetNameForUI(DTE dte)
        {
            object target = DteHelper.GetTarget(dte);
            ProjectItem projectItem = (target as ProjectItem).Collection.Parent as ProjectItem;

            if (projectItem != null)
            {
                return SolutionHelper.GetTargetPathOfProjectItem(projectItem).Replace("\\", ".");
            }

            return string.Empty;
        }

        public static void SelectItemInExplorer(DTE vs, object target)
        {
            ProjectItem projectItem = target as ProjectItem;
            if (projectItem != null)
            {
                StringBuilder pathBuilder = new StringBuilder(SolutionHelper.FindProjectUIPath(vs, projectItem.ContainingProject));
                BuildProjectItemPath(pathBuilder, projectItem);
                SelectItemInExplorer(vs, pathBuilder.ToString());
            }
        }

        public static void SelectItemInExplorer(DTE vs, string itemPath)
        {
            Window solutionExplorer = vs.Windows.Item(Constants.vsWindowKindSolutionExplorer);
            solutionExplorer.Activate();
            solutionExplorer.SetFocus();
            UIHierarchy uih = (UIHierarchy)solutionExplorer.Object;
            UIHierarchyItem uihitem = uih.GetItem(itemPath);
            uihitem.Select(vsUISelectionType.vsUISelectionTypeSelect);
        }

        private static void BuildProjectItemPath(StringBuilder pathBuilder, ProjectItem projectItem)
        {
            if (projectItem.Collection != null)
            {
                ProjectItem parentItem = projectItem.Collection.Parent as ProjectItem;
                if (parentItem != null)
                {
                    BuildProjectItemPath(pathBuilder, parentItem);
                }
            }
            pathBuilder.Append('\\').Append(projectItem.Name);
        }

        public static void ShowMessage(IUIService uiService, string msg)
        {
            if (uiService != null)
            {
                uiService.ShowMessage(msg);
            }
            else
            {
                System.Windows.Forms.MessageBox.Show(msg);
            }
        }
    }

}
